package com.xltt.watchclient;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;

import com.xltt.watchclient.module.WatchApiHelper;
import com.xltt.watchclient.module.service.NotificationMonitor;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    WatchApiHelper mWatchApiHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWatchApiHelper = new WatchApiHelper();
        mWatchApiHelper.onCreate(this);
        LogTool.d("onCreate()");
        startService(new Intent(this,NotificationMonitor.class));
    }



    @Override
    protected void onResume() {
        super.onResume();
        BluetoothAdapter blueadapter = BluetoothAdapter.getDefaultAdapter();
        if(blueadapter == null){
            Toast.makeText(this,"手机不支持蓝牙",Toast.LENGTH_SHORT).show();
            return;
        }
        if(!blueadapter.isEnabled()){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示");
            builder.setMessage("手机蓝牙未打开，需要去打开蓝牙？");
            builder.setCancelable(false);
            builder.setPositiveButton("打开蓝牙", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    blueadapter.enable();
                    dialogInterface.dismiss();
                }
            });
            builder.show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWatchApiHelper.stopScan();
        mWatchApiHelper.onDestroy(this);
        stopService(new Intent(this,NotificationMonitor.class));
    }


    public void startScan() {
        mWatchApiHelper.startScan();
    }

    public void tryConnect(String address) {
        mWatchApiHelper.tryConnect(address);

    }


    public void checkState() {
        mWatchApiHelper.checkState();

    }

    public void activeDevice(String name, String pwd) {
        mWatchApiHelper.activeDevice(name, pwd);
    }

}
